package com.wj.facedetect.activity.alarm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wj.facedetect.R;
import com.wj.facedetect.activity.base.BaseActivity;
import com.wj.facedetect.bean.alarm.GetAlarmDetailBean;
import com.wj.facedetect.bean.alarm.RefuseTaskBean;
import com.wj.facedetect.net.ReqUrls;
import com.wj.facedetect.presenter.AlarmPresenter;
import com.wj.facedetect.util.DialogUtil;
import com.wj.facedetect.util.PreferencesUtils;
import com.wj.facedetect.util.TimeUtils;
import com.wj.facedetect.util.ToastUtil;
import com.wj.facedetect.view.IBaseView;

import java.math.BigDecimal;
import java.util.HashMap;

public class AlarmDetailActivity extends BaseActivity implements View.OnClickListener, IBaseView {
    private SimpleDraweeView simpleDraweeViewHead1, simpleDraweeViewHead2;
    private TextView tvSim, tvAddr, tvTime, tvXyfName, tvIdCardNo, tvKuName, tvBz;
    private TextView tvLeft, tvRight, tvTitle;
    private TextView tvZprw, tvJrw, tvRwzt, tvJjrw;
    private SimpleDraweeView simpleDraweeViewCjz1, simpleDraweeViewCjz2;
    private String alarmId;
    private AlarmPresenter alarmPresenter;
    private GetAlarmDetailBean getAlarmDetailBean;
    private RelativeLayout rl1, rl2, rl3, rl4;
    private String status;
    private String tag;
    private static final int REQ_REFUSE_TASK = 0x001;
    private static final int REQ_GET_AlARMDETAIL = REQ_REFUSE_TASK + 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_detail);
        alarmId = getIntent().getStringExtra("message");
        tag = getIntent().getStringExtra("tag");
        alarmPresenter = new AlarmPresenter(this);
        initView();
        requestData();
    }

    private void initView() {
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvRight = (TextView) findViewById(R.id.tv_right);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        if ("alarm".equals(tag)) {
            tvTitle.setText("报警详情");
        } else {
            tvTitle.setText("任务详情");
        }
        tvLeft.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        simpleDraweeViewHead1 = (SimpleDraweeView) findViewById(R.id.iv_head_1);
        simpleDraweeViewHead2 = (SimpleDraweeView) findViewById(R.id.iv_head_2);
        simpleDraweeViewCjz1 = (SimpleDraweeView) findViewById(R.id.iv_head_cjz_1);
        simpleDraweeViewCjz2 = (SimpleDraweeView) findViewById(R.id.iv_head_cjz_2);
        tvSim = (TextView) findViewById(R.id.tv_sim);
        tvAddr = (TextView) findViewById(R.id.tv_addr);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvXyfName = (TextView) findViewById(R.id.tv_kzp_name);
        tvIdCardNo = (TextView) findViewById(R.id.tv_idcard);
        tvKuName = (TextView) findViewById(R.id.tv_ssk);
        tvBz = (TextView) findViewById(R.id.tv_bz);
        tvZprw = (TextView) findViewById(R.id.tv_zprw);
        tvJrw = (TextView) findViewById(R.id.tv_jrw);
        tvRwzt = (TextView) findViewById(R.id.tv_rwzt);
        tvJjrw = (TextView) findViewById(R.id.tv_jjrw);
        tvJjrw.setOnClickListener(this);
        tvZprw.setOnClickListener(this);
        tvJrw.setOnClickListener(this);
        tvRwzt.setOnClickListener(this);
        rl1 = (RelativeLayout) findViewById(R.id.rl_1);
        rl2 = (RelativeLayout) findViewById(R.id.rl_2);
        rl3 = (RelativeLayout) findViewById(R.id.rl_3);
        rl4 = (RelativeLayout) findViewById(R.id.rl_4);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                break;
            case R.id.tv_zprw:
                //指派任务
//                Intent intentZprw = new Intent(this, CreateGroupActivity.class);
//                intentZprw.putExtra("alarmId", alarmId);
//                startActivityForResult(intentZprw, 112);
                break;
            case R.id.tv_jrw:
                Intent intentJrw = new Intent(this, DealAlarmActivity.class);
                intentJrw.putExtra("alarmId", alarmId);
                startActivityForResult(intentJrw, 113);
                break;
            case R.id.tv_jjrw:
                DialogUtil.showInputJjRwBz(this, new DialogUtil.SettingIpListener() {
                    @Override
                    public void getIp(String zwIp, String gwIp, String id) {
                        HashMap<String, String> params = new HashMap<String, String>();
                        params.put("warnId", alarmId);
                        params.put("user", PreferencesUtils.getInstance().getSharePreInt(PreferencesUtils.USER_ID) + "");
                        params.put("desc", zwIp);
                        showProgressDialog();
                        alarmPresenter.doRequestPostTaskRefuse(params, REQ_REFUSE_TASK, this);
                    }
                });
                break;
            case R.id.tv_rwzt:
                Intent intentRwzt = new Intent(this, AlarmStatusActivity.class);
                intentRwzt.putExtra("status", status);
                intentRwzt.putExtra("warnId", alarmId);
                startActivityForResult(intentRwzt, 111);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 111 && RESULT_OK == resultCode) {
            finish();
        } else if (requestCode == 112 && RESULT_OK == resultCode) {
            finish();
        } else if (requestCode == 113 && RESULT_OK == resultCode) {
            setResult(RESULT_OK);
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void requestData() {
        showProgressDialog();
        alarmPresenter.doRequestGetAlarmDetail(alarmId, PreferencesUtils.getInstance().getSharePreInt(PreferencesUtils.USER_ID) + "", REQ_GET_AlARMDETAIL, this);
    }


    private void setData() {
        if (getAlarmDetailBean != null) {
            GetAlarmDetailBean.DataBean data = getAlarmDetailBean.getData();
            if (data != null) {
                if (!TextUtils.isEmpty(data.getAcquiredFaceImg())) {
                    String photoUrl1Temp = data.getAcquiredFaceImg().replace("\n", "");
                    simpleDraweeViewHead1.setImageURI(PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL)
                            + String.format(ReqUrls.URL_GET_PHOTO2, data.getClusterId(), photoUrl1Temp));
                }
                if (!TextUtils.isEmpty(data.getDatabaseFaceImg())) {
                    String photoUrl1Temp = data.getDatabaseFaceImg().replace("\n", "");
                    //simpleDraweeViewHead2.setImageURI(PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL) + ReqUrls.URL_GET_PHOTO + photoUrl1Temp);
                    simpleDraweeViewHead2.setImageURI(PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL)
                            + String.format(ReqUrls.URL_GET_PHOTO2, data.getClusterId(), photoUrl1Temp));
                }
                if (!TextUtils.isEmpty(data.getAcquiredPicImg())) {
                    String photoUrl1Temp = data.getAcquiredPicImg().replace("\n", "");
                    simpleDraweeViewCjz1.setImageURI(PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL)
                            + String.format(ReqUrls.URL_GET_PHOTO2, data.getClusterId(), photoUrl1Temp));
                }
                if (!TextUtils.isEmpty(data.getPersonName())) {
                    tvXyfName.setText(data.getPersonName());
                }
                if (!TextUtils.isEmpty(data.getRepositoryName())) {
                    tvKuName.setText(data.getRepositoryName());
                }
                BigDecimal b = data.getHitPrecent();
                if (b != null) {
                    double ppd = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    tvSim.setText(ppd + "");
                }
                long time = data.getTime();
                long timeLong = time * 1000;
                String formatTimeByType = TimeUtils.getFormatTimeByType(timeLong, TimeUtils.DATE_FORMAT_MMDDHHMM);
                tvTime.setText(formatTimeByType);
                if (!TextUtils.isEmpty(data.getAddr())) {
                    tvAddr.setText(data.getAddr());
                }
                if (!TextUtils.isEmpty((String) data.getSeat())) {
                    tvIdCardNo.setText((String) data.getSeat());
                }
                status = data.getStatus();
                if ("alarm".equals(tag)) {
                    if ("UNTREATED".equals(data.getStatus())) {
                        rl1.setVisibility(View.VISIBLE);
                        rl2.setVisibility(View.GONE);
                        rl3.setVisibility(View.GONE);
                        rl4.setVisibility(View.VISIBLE);
                    } else {
                        rl1.setVisibility(View.GONE);
                        rl2.setVisibility(View.GONE);
                        rl3.setVisibility(View.GONE);
                        rl4.setVisibility(View.VISIBLE);
                    }
                } else {
                    if ("COMPLETION".equals(data.getStatus())) {
                        rl1.setVisibility(View.GONE);
                        rl2.setVisibility(View.GONE);
                        rl3.setVisibility(View.GONE);
                        rl4.setVisibility(View.VISIBLE);
                    } else if ("UNTREATED".equals(data.getStatus())) {
                        rl1.setVisibility(View.VISIBLE);
                        rl2.setVisibility(View.GONE);
                        rl3.setVisibility(View.GONE);
                        rl4.setVisibility(View.VISIBLE);
                    } else if ("PROCESSING".equals(data.getStatus())) {
                        rl1.setVisibility(View.GONE);
                        rl2.setVisibility(View.VISIBLE);
                        rl3.setVisibility(View.VISIBLE);
                        rl4.setVisibility(View.VISIBLE);
                    }
                }

            }

        }
    }

    @Override
    public void onSuccess(int what, Object data, Object carryObj) {
        dismissProgressDialog();
        switch (what) {
            case REQ_REFUSE_TASK:
                RefuseTaskBean refuseTaskBean = (RefuseTaskBean) data;
                if (refuseTaskBean != null) {
                    if (refuseTaskBean.isSuccess()) {
                        DialogUtil.showErrorMsgWithBack(this, "任务已拒接", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
                    } else {
                        DialogUtil.showErrorMsg(this, refuseTaskBean.getErrorMessage());
                    }
                }
                break;
            case REQ_GET_AlARMDETAIL:
                GetAlarmDetailBean getAlarmDetailBeanx = (GetAlarmDetailBean) data;
                if (getAlarmDetailBeanx != null) {
                    if (getAlarmDetailBeanx.isSuccess()) {
                        getAlarmDetailBean = getAlarmDetailBeanx;
                        setData();
                    }
                }
                break;
        }
    }

    @Override
    public void onError(int what, Exception e, Object carryObj) {
        dismissProgressDialog();
        ToastUtil.showMessage("网络异常");
    }

    @Override
    public void onFail(int what, int errorCode, String errorMsg, Object carryObj) {
        dismissProgressDialog();
    }
}
